/* FILE = RocketView.m */

#import <appkit/appkit.h>
#import "rocketview.h"

@implementation  RocketView : View
{	
	/* repeating the instance-variables part of the object declaration is not 
required by the syntax, but it is permitted and checked to match the interface 
declaration, and I think it is a good idea for documentation purposes. */

	NXPoint	misslePositions[MAXMISSLES];
	BOOL		misslesLaunched;
	int		numRockets;

	id		BlaunchRockets; 
	/* launch rocket button */

	id		IFnumberRockets; 
	/* input text field = a Form object */
}

+ newFrame: (const NXRect *) frameRect
	/* override View's default newFrame method */
{
	int i;

	self = [ super newFrame: frameRect ]; 

/* change self to point to the newly created object-instance, instead of 
pointing to the rocketView Factory object. */

	numRockets = 0;
	for ( i = 0; i < MAXMISSLES ; i++ )
	{
		misslePositions[i].x = (1+i) * 10;
		misslePositions[i].y = 10.0;
	}
	misslesLaunched = NO;

	[self setFlip: NO]; 
	/* keep 0,0 at lower left corner of view */
		
	[self allocateGstate];
	return self; 
}

- updatedrawing
	/* draws the missles in the view */
{
	int i;

	[self lockFocus ];

	if ( misslesLaunched )
	{
		for (i = 0; i < numRockets - 1; i++ )
		{	
			/* draw Very simple missles Ñ not 
				even animated! */
			PSmoveto( misslePositions[i].x, 
				misslePositions[i].y );
			PSlineto( misslePositions[i].x, 
				misslePositions[i].y + 10.0 );
			PSstroke();
		}
	}
	NXPing(); 

	/* NXPing forces the window manager to update the view on screen -- flushes 
the postscript pipeline */

	[self unlockFocus];

	return self; 

	/* if nothing else, tradition says return self */
}

- drawSelf: (const NXRect *) rects : (int) rectCount
	/* override View's default drawing method */
{
	/* ignore the rectangles which could be used for limiting the amount of 
drawing required. */

	[ self updatedrawing ];
	return self;
}

- setBlaunchRockers:anObject
	/* created by interface builder */
{
	BlaunchRockers = anObject;
	return self;
}

- setIFnumberRockets:anObject
	/* created by interface builder */
{
	IFnumberRockets = anObject;
	return self;
}

- rocketlaunch:sender
	/* in this program, this sender will always be the button named "Launch 
Rockets" and this routine will be called when the user clicks in this button 
object during execution of this program */
{
	int i;

	if ( ! misslesLaunched )
	{
		misslesLaunched = YES;
		i = [ IFnumberRockets intValueAt: 0 ];

		/* get the number of rockets from the string contained in the Form.  We 
specified an initial default value with Interface Builder; at any time before 
pushing the button, the user could change the text to any string.  If an integer 
can not be parsed from the string, we get zero as the value. */
			
		if ( i <= MAXMISSLES )
		{
			numRockets = i;
		}
	}
	else
	{
		NXBeep();
	}
	[ self updatedrawing ];
}

@end